package com.example.mylibrary.util;

import java.math.BigDecimal;

/**
 * Created by Administrator on 2018/3/26.
 */

/**
 * 根据Byte获取大小
 */
public class CommonUtil {
    public static String getStingSizeFromByte(long bytes) {
        if (bytes < 1024) {
            return bytes + " B";
        }
        if (bytes < 1024 * 1024) {
            return new BigDecimal(bytes / ((double) 1024)).setScale(2,
                    BigDecimal.ROUND_HALF_DOWN) + " KB";
        }
        if (bytes < 1024 * 1024 * 1024) {
            return new BigDecimal(bytes / ((double) (1024 * 1024))).setScale(2,
                    BigDecimal.ROUND_HALF_DOWN) + " MB";
        }
        return null;
    }
}
